import { ERoleStatus } from 'repos-model';
import { ApiProperty } from '@nestjs/swagger';
import {
	BeforeUpdate,
	BeforeInsert,
	Column,
	CreateDateColumn,
	Entity,
	PrimaryColumn,
	UpdateDateColumn,
} from 'typeorm';

@Entity()
export class User {
	constructor(props?: Partial<User>) {
		Object.assign(this, props);
	}

	@PrimaryColumn()
	public telegramId!: string;

	@ApiProperty({ example: 'smirnova', description: 'login' })
	@Column({
		unique: true,
		nullable: true,
	})
	public username!: string;

	@ApiProperty({ example: 'smirnova', description: 'login' })
	@Column({
		nullable: true,
		unique: true,
	})
	public nickname!: string;

	@ApiProperty({ example: 'smirnova', description: 'login' })
	@Column({
		type: 'integer',
		default: 0,
	})
	public balance!: number;

	@BeforeInsert()
	@BeforeUpdate()
	checkBalance() {
		if (this.balance < 0) {
			throw new Error('Balance cannot be negative');
		}
	}

	@ApiProperty({ example: 'smirnova', description: 'login' })
	@Column({
		nullable: true,
	})
	public firstName!: string;

	@ApiProperty({ example: 'smirnova', description: 'login' })
	@Column({
		nullable: true,
	})
	public lastName!: string;

	@ApiProperty({ example: 'smirnova', description: 'login' })
	@Column({
		nullable: true,
	})
	public description!: string;

	@ApiProperty({ example: 'LEMMING', description: 'role' })
	@Column({
		type: 'enum',
		enum: ERoleStatus,
		default: ERoleStatus.User,
	})
	public role!: ERoleStatus;

	@ApiProperty({ example: 'smirnova', description: 'login' })
	@Column({
		nullable: true,
	})
	public apiKey!: string;

	@ApiProperty({ example: 'smirnova', description: 'login' })
	@Column({
		nullable: true,
	})
	public secretKey!: string;

	@ApiProperty({ example: '', description: 'Время добавления api keys' })
	@Column('timestamptz', {
		nullable: true,
	})
	public dateAddKey!: Date;

	@ApiProperty({ example: 'validKey', description: 'validKey' })
	@Column({
		nullable: true,
		default: false,
	})
	public validKey!: boolean;

	@ApiProperty({ example: 'validKey', description: 'validKey' })
	@Column({
		nullable: true,
		default: false,
	})
	public watchOrders!: boolean;

	@ApiProperty({ example: 'validKey', description: 'validKey' })
	@Column({
		nullable: true,
	})
	public referralCode!: string;

	@ApiProperty({ example: 'smirnova', description: 'login' })
	@CreateDateColumn({ nullable: true, name: 'created_at' })
	createdAt!: Date;

	@ApiProperty({ example: '', description: 'Время последнего обновления' })
	@UpdateDateColumn({ nullable: true, name: 'updated_at' })
	updatedAt!: Date;
}
